package com.bpr.webservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpr.webservice.controller.util.Constants;
import com.bpr.webservice.domain.Usergroups;
import com.bpr.webservice.domain.dto.UsergroupDto;
import com.bpr.webservice.domain.dto.UsergroupPageDto;
import com.bpr.webservice.repository.UsergroupsRepository;

@Controller
@RequestMapping("/usergroups")
public class UsergroupController {

	@Autowired UsergroupsRepository usergroupsRepository;
	
	@RequestMapping(value="/save", method = RequestMethod.POST )
	public ResponseEntity<String> doSave(
			@RequestBody Usergroups usergroups
			){
		try {
			usergroupsRepository.save(usergroups);		
		} catch (Exception ex){
			return new ResponseEntity<String>(ex.getMessage(), HttpStatus.EXPECTATION_FAILED);
		}
		
		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.SAVE_SUCCED, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<String> doDelete(@PathVariable("id") Long id){
		try {
			usergroupsRepository.delete(id);
		} catch (Exception ex){
			return new ResponseEntity<String>(ex.getMessage(), HttpStatus.EXPECTATION_FAILED);
		}
		
		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.DELETE_SUCCED, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/deletes", method = RequestMethod.POST)
	public ResponseEntity<String> doDeleteMoreThanOne(@RequestBody String[] ids) {
		try {
			for(int i=0;i < ids.length;i++){
				usergroupsRepository.delete(Long.valueOf(ids[i]));
			}
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.DELETE_SUCCED,
				HttpStatus.OK);
	}
	
	@RequestMapping(value = "/findone/{id}", method = RequestMethod.GET)
	public @ResponseBody UsergroupDto getOne(@PathVariable("id") Long id){
		Usergroups group = usergroupsRepository.findOne(id);
		UsergroupDto dto = new UsergroupDto();
		if (group != null){
			dto.constructDto(group);
		}
		return dto;
	}
	
	@RequestMapping(value = "/search")
	public @ResponseBody Page<UsergroupDto> findAll(@RequestParam("page") Integer page,
			@RequestParam("limit") Integer limit) {
		Page<Usergroups> search = usergroupsRepository.findAll(new PageRequest(page - 1, limit));
		Page<UsergroupDto> result = new UsergroupPageDto(search);
		return result;
	}
	
	@RequestMapping(value = "/findByNameContains")
	public @ResponseBody Page<UsergroupDto> findByNameContains(
			@RequestParam("usergroupname") String name,
			@RequestParam("page") Integer pagenum,
			@RequestParam("limit") Integer limit			
			){
		PageRequest page = new PageRequest(pagenum - 1, limit);
		Page<Usergroups> searchResult = usergroupsRepository.findUsergroupByGroupname("%" + name + "%", page);
		Page<UsergroupDto> dtoResult = new UsergroupPageDto(searchResult);
		System.out.println("** number["+searchResult.getContent().size()+"]");
		return dtoResult;
	}
	
	@RequestMapping(value = "/form.html")
	public String doForm() {

		return "usergroups_form";
	}

	@RequestMapping(value = "/list.html")
	public String doList() {
		return "usergroups_list";
	}
}
